package com.google.way1;

import java.util.List;
import java.util.Map;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class AdapterForLinearLayout extends BaseAdapter {
	
	private LayoutInflater layoutInflater;
	private int resource;
	private List<Map<String, String>> list;
	private String from[];
	private int[] to;
	
	public AdapterForLinearLayout(Context context, int resource, List<Map<String, String>> list,
			String[] from, int[] to){
		this.resource = resource;
		this.list = list;
		this.from = from;
		this.to = to;
		layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		
		convertView = layoutInflater.inflate(resource, null);
		Map<String, String> map = list.get(position);
		int count = to.length;
		for(int i=0;i<count;i++){
			View v = convertView.findViewById(to[i]);
			bindView(v, map, from[i]);
		}
		convertView.setTag(position);
		
		return convertView;
	}
	
	private void bindView(View view, Map<String, String> map, String form){
		String text = map.get(form);
		if(view instanceof TextView){
			((TextView)view).setText(text);
		}
	}

}
